package words;

import java.util.*;
import java.io.*;


public class TagDefinition implements Serializable {
  
  HashMap m_mapNames;
  Vector m_vecNames;

  public TagDefinition()
  {
    m_mapNames = new HashMap();
    m_vecNames = new Vector();
  }

  public boolean load(String strFile)
  {
    try {
      BufferedReader file = new BufferedReader(new FileReader(strFile));

      // read file line by line
      String strLine = file.readLine();
      while (strLine != null) {
	strLine = strLine.trim().toLowerCase();
	// add tag name in repository
	if (strLine.length() > 0 && !m_mapNames.containsKey(strLine)) {
	  m_vecNames.add(strLine);
	  m_mapNames.put(strLine, new Integer(m_vecNames.size() - 1));
	}

        strLine = file.readLine();
      }
    }
    catch (IOException e) {
      System.out.println(e);
      return false;
    }
    
    return true;  
  }

  public String getName(int nId)
  {
    if (nId >= 0 && nId < m_vecNames.size())
      return (String) m_vecNames.get(nId);
    else
      return null;
  }

  public int getId(String strName)
  {
    Integer intId = (Integer) m_mapNames.get(strName.toLowerCase());
    if (intId != null)
      return intId.intValue();
    else
      return -1;
  }

  public String toString()
  {
    String strTags = new String();
    for (int i = 0; i < m_vecNames.size(); i++)
      strTags += (String) m_vecNames.get(i) + "\n";

    return strTags;
  }

  public static void main(String[] args)
  {
    if (args.length != 1) {
      System.out.println("Usage: java TagDefinition <tag-file>");
      System.exit(1);
    }

    TagDefinition tagDef = new TagDefinition();
    tagDef.load(args[0]);

    System.out.println(tagDef);
  }
}
